import redis
import sys
import time
from elasticsearch import Elasticsearch
import logging

# 定义redis连接
r = redis.ConnectionPool(host='192.168.0.67', port=6379, db=0, decode_responses=True)

# ES 连接
_es = Elasticsearch(["192.168.1.78:9200"],
                    http_auth=('elastic', '1CHYC5IO6TrgZM66kyOw'),
                    # sniff before doing anything
                    sniff_on_start=True,
                    # refresh nodes after a node fails to respond
                    sniff_on_connection_fail=True,
                    # and also every 60 seconds
                    sniffer_timeout=60)


def es_query_sum(start_, end_):
    domain_name = '{}.otosaas.com'.format(app_code)
    requset_status = [400, 401, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 500, 501,
                      502, 503, 504, 505]
    # {
    # "aggs": {
    #     "2": {
    #         "date_histogram": {
    #             "field": "timestamp",
    #             "interval": "3600m",
    #             "time_zone": "Asia/Shanghai",
    #             "min_doc_count": 1
    #         }
    #     }
    # },
    # "stored_fields": [
    #     "*"
    # ],
    # "script_fields": {},
    # "docvalue_fields": [
    #     {
    #         "field": "@timestamp",
    #         "format": "date_time"
    #     },
    #     {
    #         "field": "timestamp",
    #         "format": "date_time"
    #     }
    # ],
    # "query": {
    #     "bool": {
    #         "must": [
    #             {
    #                 "match_all": {}
    #             },
    #             {
    #                 "match_phrase": {
    #                     "http_appCode": {
    #                         "query": app_code
    #                     }
    #                 }
    #             },
    #             {
    #                 "range": {
    #                     "request_status": {
    #                         "gte": 404,
    #                         "lt": 405
    #                     }
    #                 },
    #             },
    #             {
    #                 "range": {
    #                     "timestamp": {
    #                         "gte": 1677340800000,
    #                         "lte": 1677427199999,
    #                         "format": "epoch_millis"
    #                     }
    #                 }
    #             }
    #           ]
    #         }
    #     }
    # }
    for i in requset_status:
        es_search_body = {
            "_source": {
                "excludes": []
            },
            "aggs": {
                "2": {
                    "date_histogram": {
                        "field": "timestamp",
                        "interval": "1800m",
                        "time_zone": "Asia/Shanghai",
                        "min_doc_count": 1
                    }
                }
            },
            "stored_fields": [
                "*"
            ],
            "script_fields": {},
            "docvalue_fields": [
                {
                    "field": "@timestamp",
                    "format": "date_time"
                },
                {
                    "field": "timestamp",
                    "format": "date_time"
                }
            ],
            "query": {
                "bool": {
                    "must": [
                        {
                            "match_all": {}
                        },
                        {
                            "match_phrase": {
                                "request_status": {
                                    "query": i
                                }
                            }
                        },
                        {
                            "match_phrase": {
                                "httphost": {
                                    "query": "ceb.otosaas.com"
                                }
                            }
                        },
                        {
                            "range": {
                                "timestamp": {
                                    "gte": 1677394800000,
                                    "lte": 1677396600000,
                                    "format": "epoch_millis"
                                }
                            }
                        }
                    ],
                    "filter": [],
                    "should": [],
                    "must_not": []
                }
            }
        }
        # 执行es查询
        data = _es.search(index=f'otosaas_qlllog-*', body=es_search_body)
        #     print(data)
        # print(data['aggregations']['2'])
        # ckk = 0
        try:
            ckk = data['aggregations']['2']['buckets'][-1]['doc_count']
            # print(data)
            # print(ckk(type))
            # print(ckk)
        except IndexError:
            ckk = 0
        print(ckk)

        # print(data)
        # [-1]['doc_count']

    # print(ckk[-1]['doc_count'])
    # print(data['hits']['total'])
    # print(data['aggregations']['2']['buckets']['200.0-599.0']['5']['buckets'])

    # print(data['aggregations']['3']['buckets']['400.0-599.0']['5']['buckets'])
    # print(data['aggregations']['3']['buckets']['200.0-599.0']['5']['buckets'])
    try:
        for key, value in data['aggregations']['3']['buckets']['200.0-599.0']['5']['buckets'].items():
            if '1677319200000' in key:
                count_4xx = value['doc_count']
            else:
                continue
            # elif '400' in key:
            #     count_4xx = value['doc_count']
            # else:
            #     break
    except:
        logging.info('返回值报错')
    a = redis.Redis(connection_pool=r)
    a.set('ELKRequestStatus-{}-{}'.format(app_code,status_level), count_4xx)
    # return data['hits']['total'] ,data['aggregations']['UV']['value']


# def get_fetch_slow_from_redis(app_code, status_level):
#     '''
#         根据 appCode 、status_level 获取相应的redis数据
#         2020-07-13
#     '''
#     a = redis.Redis(connection_pool=r)
#     if status_level == '3xx':
#         resp = a.get('ELKRequestStatus-{}-3xx'.format(app_code)) or 0
#     elif status_level == '4xx':
#         resp = a.get('ELKRequestStatus-{}-4xx'.format(app_code)) or 0
#     elif status_level == '5xx':
#         resp = a.get('ELKRequestStatus-{}-5xx'.format(app_code)) or 0
#     else:
#         resp = 0
#     return resp
#
#     self.rdb.set('ELKRequestStatus-{}-4xx'.format(app_code), count_4xx)
    # self.rdb.set('ELKRequestStatus-{}-5xx'.format(app_code), count_5xx)


if __name__ == "__main__":
    app_code = 'bhbank'
    status_level = '404'
    # today_date = '2023.02.24'
    start_ = 1677340800000
    end_ = 1677427199999
    es_query_sum(start_, end_)
    # count = get_fetch_slow_from_redis(app_code, status_level)
    # print(count)
